package com.ruixun.controller;

import com.ruixun.common.PageData;
import com.ruixun.common.ResponseResult;
import com.ruixun.entity.SysPermission;
import com.ruixun.query.SysPermissionPageQuery;
import com.ruixun.rvo.PermissionStatsRvo;
import com.ruixun.rvo.SysMenuRvo;
import com.ruixun.rvo.SysPermissionRvo;
import com.ruixun.service.SysPermissionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 权限管理控制器
 * @author ruixun
 */
@Api(tags = "权限管理接口")
@RestController
@RequestMapping("/permission")
@RequiredArgsConstructor
@Slf4j
public class SysPermissionController {

    private final SysPermissionService permissionService;

    @ApiOperation(value = "分页查询权限列表", notes = "支持按权限名称模糊查询和权限类型等值查询")
    @PostMapping("/page")
    public ResponseResult<PageData<SysPermissionRvo>> getPermissionPage(@Valid @RequestBody SysPermissionPageQuery query) {
        PageData<SysPermissionRvo> pageData = permissionService.getPermissionPage(query);
        return ResponseResult.success(pageData);
    }

    @ApiOperation(value = "新增权限", notes = "新增权限信息")
    @PostMapping("/add")
    public ResponseResult<Boolean> addPermission(@Valid @RequestBody SysPermission permission) {
        boolean result = permissionService.addPermission(permission);
        return result ? ResponseResult.success(true) : ResponseResult.fail("新增权限失败");
    }


    @ApiOperation(value = "获取当前用户的权限")
    @GetMapping("/user")
    public ResponseResult<List<SysPermissionRvo>> userPermission() {
        List<SysPermissionRvo> dataList = permissionService.userMenu();
        return ResponseResult.success(dataList);
    }

    @ApiOperation(value = "修改权限", notes = "修改权限信息")
    @PostMapping("/update")
    public ResponseResult<Boolean> updatePermission(@Valid @RequestBody SysPermission permission) {
        if (permission.getId() == null) {
            return ResponseResult.fail("权限ID不能为空");
        }
        boolean result = permissionService.updatePermission(permission);
        return result ? ResponseResult.success(true) : ResponseResult.fail("修改权限失败");
    }

    @ApiOperation(value = "删除权限", notes = "根据ID删除权限（逻辑删除）")
    @PostMapping("/delete/{id}")
    public ResponseResult<Boolean> deletePermission(@ApiParam(value = "权限ID", required = true) @PathVariable Long id) {
        boolean result = permissionService.deletePermission(id);
        return result ? ResponseResult.success(true) : ResponseResult.fail("删除权限失败");
    }

    @ApiOperation(value = "获取权限详情", notes = "根据ID获取权限详细信息")
    @GetMapping("/detail/{id}")
    public ResponseResult<SysPermissionRvo> getPermissionDetail(@ApiParam(value = "权限ID", required = true) @PathVariable Long id) {
        SysPermissionRvo permission = permissionService.getPermissionById(id);
        return permission != null ? ResponseResult.success(permission) : ResponseResult.fail("权限不存在");
    }


    @ApiOperation(value = "获取权限统计数量", notes = "获取权限统计数量")
    @GetMapping("/stats")
    public ResponseResult<PermissionStatsRvo> stats() {
        PermissionStatsRvo permissionStatsRvo = permissionService.stats();
        return ResponseResult.success(permissionStatsRvo);
    }

    @ApiOperation(value = "获取权限统计数量", notes = "获取权限统计数量")
    @GetMapping("/all")
    public ResponseResult<List<SysPermissionRvo>> all() {
        List<SysPermissionRvo> dataList = permissionService.all();
        return ResponseResult.success(dataList);
    }

}